import datetime
from applications.extensions import db


class User(db.Model):
    __tablename__ = 'user'

    # 定义表的列
    uid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    email = db.Column(db.String(50), nullable=True, comment='邮箱')
    mobile = db.Column(db.String(11), nullable=True, comment='手机号')
    username = db.Column(db.String(50), nullable=False, comment='用户名')
    group_id = db.Column(db.Integer, default=2, nullable=False, comment='用户组')
    avatar = db.Column(db.String(255), nullable=True, comment='头像')
    gender = db.Column(db.Integer, default=0, nullable=False, comment='性别(0未知，1男，2女)')
    province = db.Column(db.String(50), default='', nullable=False, comment='省份')
    city = db.Column(db.String(20), default='', nullable=False, comment='城市')
    openid = db.Column(db.String(255), nullable=True, comment='小程序openid')
    mp_openid = db.Column(db.String(100), nullable=True, comment='公众号openid')
    unionid = db.Column(db.String(100), nullable=True, comment='unionid')
    status = db.Column(db.Integer, default=0, nullable=False, comment='状态')
    intro = db.Column(db.String(255), default='这个人很懒，没留下什么', nullable=True, comment='个性签名')
    money = db.Column(db.Numeric(8, 2), default=0.00, nullable=False, comment='用户余额')
    sign_num = db.Column(db.Integer, default=0, nullable=False, comment='连续签到天数')
    integral = db.Column(db.Integer, default=0, nullable=True, comment='积分')
    last_login_ip = db.Column(db.String(20), nullable=True, comment='最后登录ip')
    tag_str = db.Column(db.JSON, default='', nullable=False, comment='用户标签')
    vip = db.Column(db.Integer, default=0, nullable=True, comment='0普通用户1会员')
    vip_expire_time = db.Column(db.DateTime, nullable=True, comment='会员过期时间')
    type = db.Column(db.Integer, default=0, nullable=True, comment='0为普通用户，1官方账号2马甲虚拟用户')
    voice_signature_id = db.Column(db.Integer, default=0, nullable=True, comment='语音签名信息的语音id')
    popular = db.Column(db.String(255), default=0, nullable=True, comment='人气')
    update_time = db.Column(db.DateTime, nullable=True, default=datetime.datetime.now, onupdate=datetime.datetime.now,
                            comment='更新时间')
    create_time = db.Column(db.DateTime, nullable=True, default=datetime.datetime.now, comment='创建时间')
    level = db.Column(db.Integer, default=1, nullable=True, comment='用户等级')


    def to_dict(self):
        """将用户实例转换为字典"""
        return {
            'uid': self.uid,
            'email': self.email,
            'mobile': self.mobile,
            'username': self.username,
            'groupId': self.group_id,
            'avatar': self.avatar,
            'gender': self.gender,
            'province': self.province,
            'city': self.city,
            'openid': self.openid,
            'mpOpenid': self.mp_openid,
            'unionid': self.unionid,
            'status': self.status,
            'intro': self.intro,
            'money': str(self.money),  # 注意将 decimal 转换为字符串
            'signNum': self.sign_num,
            'integral': self.integral,
            'lastLoginIp': self.last_login_ip,
            'tagStr': self.tag_str,
            'vip': self.vip,
            'vipExpireTime': self.vip_expire_time.isoformat() if self.vip_expire_time else None,
            'type': self.type,
            'voiceSignatureId': self.voice_signature_id,
            'popular': self.popular,
            'updateTime': self.update_time.isoformat() if self.update_time else None,
            'createTime': self.create_time.isoformat() if self.create_time else None,
            'level': self.level
        }